/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
package com.ruoyi.mall.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.mall.api.RemoteGoodsSpuService;
import com.ruoyi.mall.api.entity.GoodsSpu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * spu商品
 *
 * @author JL
 * @date 2019-08-12 16:25:10
 */
@Slf4j
@RestController
@RequestMapping("/goodsspu")
@Api(value = "goodsspu", tags = "spu商品管理")
public class GoodsSpuController extends BaseController {
    @DubboReference
    private RemoteGoodsSpuService goodsSpuService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param goodsSpu spu商品
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    @SaCheckPermission("mall:goodsspu:index")
    public R getGoodsSpuPage(Page page, GoodsSpu goodsSpu) {
        return R.ok(goodsSpuService.page1(page, goodsSpu));
    }

    /**
     * list查询
     *
     * @param goodsSpu
     * @return
     */
    @ApiOperation(value = "list查询")
    @GetMapping("/list")
    public List<GoodsSpu> getList(GoodsSpu goodsSpu) {
        return goodsSpuService.listByIdAndName(goodsSpu);
    }

    /**
     * 查询数量
     *
     * @param goodsSpu
     * @return
     */
    @ApiOperation(value = "查询数量")
    @GetMapping("/count")
    public R getCount(GoodsSpu goodsSpu) {
        return R.ok(goodsSpuService.count(goodsSpu));
    }

    /**
     * 通过id查询spu商品
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id查询spu商品")
    @GetMapping("/{id}")
    @SaCheckPermission("mall:goodsspu:get")
    public R getById(@PathVariable("id") String id) {
        return R.ok(goodsSpuService.getById1(id));
    }

    /**
     * 新增spu商品
     *
     * @param goodsSpu spu商品
     * @return R
     */
    @ApiOperation(value = "新增spu商品")
    @PostMapping
    @SaCheckPermission("mall:goodsspu:add")
    public R save(@RequestBody GoodsSpu goodsSpu) {
        return R.ok(goodsSpuService.save1(goodsSpu));
    }

    /**
     * 修改spu商品
     *
     * @param goodsSpu spu商品
     * @return R
     */
    @ApiOperation(value = "修改spu商品")
    @PutMapping
    @SaCheckPermission("mall:goodsspu:edit")
    public R updateById(@RequestBody GoodsSpu goodsSpu) {
        return R.ok(goodsSpuService.updateById1(goodsSpu));
    }

    /**
     * 商品上下架操作
     *
     * @param shelf
     * @param ids
     * @return R
     */
    @ApiOperation(value = "商品上下架操作")
    @PutMapping("/shelf")
    @SaCheckPermission("mall:goodsspu:edit")
    public R updateById(@RequestParam(value = "shelf") String shelf, @RequestParam(value = "ids") String ids) {
        GoodsSpu goodsSpu = new GoodsSpu();
        goodsSpu.setShelf(shelf);
        return R.ok(goodsSpuService.updateInIds(goodsSpu, Convert.toList(ids)));
    }

    /**
     * 通过id删除spu商品
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id删除spu商品")
    @DeleteMapping("/{id}")
    @SaCheckPermission("mall:goodsspu:del")
    public R removeById(@PathVariable String id) {
        return R.ok(goodsSpuService.removeById(id));
    }

}
